{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0d824285",
   "metadata": {},
   "source": [
    "# Caffe 输入层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ce6ef02c",
   "metadata": {
    "tags": [
     "remove-cell"
    ]
   },
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "from tvm_book.config import env\n",
    "# 设置 caffeprotobuf环境\n",
    "env.set_caffeproto(Path(env.__file__).parents[3]/\"tests/caffeproto\")\n",
    "# 设置tvm环境\n",
    "env.set_tvm(\"/media/pc/data/board/arria10/lxw/tasks/tvm-test\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1921b75e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "from google.protobuf import text_format\n",
    "import caffe_pb2 as pb2\n",
    "\n",
    "temp_dir = Path(\".temp\")\n",
    "temp_dir.mkdir(exist_ok=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "12caad46",
   "metadata": {},
   "outputs": [],
   "source": [
    "def parse_input(predict_net):\n",
    "    old_caffe = False\n",
    "    if len(predict_net.input) != 0:  # old caffe version\n",
    "        old_caffe = True\n",
    "        model_inputs = list(predict_net.input)\n",
    "        if len(predict_net.input_shape) !=0:\n",
    "            shapes = [list(sp.dim) for sp in predict_net.input_shape]\n",
    "        else:\n",
    "            assert len(model_inputs) == 1\n",
    "            shapes = [list(predict_net.input_dim)]\n",
    "    else:\n",
    "        model_inputs = []\n",
    "        shapes = []\n",
    "        for pl in predict_net.layer:\n",
    "            if pl.type == \"Input\":\n",
    "                assert len(pl.top) == 1, \"The number of Input layer's output is more than 1.\"\n",
    "                model_inputs.append(pl.top[0])\n",
    "                shapes.append(list(pl.input_param.shape[0].dim))\n",
    "    return model_inputs, shapes"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87bfec7d",
   "metadata": {},
   "source": [
    "输入层有两种写法："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e29dfa33",
   "metadata": {},
   "source": [
    "## 新的写法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ebfade9",
   "metadata": {},
   "source": [
    "### 单数入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fb8a2716",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "layer {\n",
       "  name: \"data\"\n",
       "  type: \"Input\"\n",
       "  top: \"data\"\n",
       "  input_param {\n",
       "    shape {\n",
       "      dim: 1\n",
       "      dim: 1\n",
       "      dim: 120\n",
       "      dim: 120\n",
       "    }\n",
       "  }\n",
       "}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text = \"\"\"\n",
    "layer {\n",
    "  name: \"data\"\n",
    "  type: \"Input\"\n",
    "  top: \"data\"\n",
    "  input_param {\n",
    "    shape: { dim: 1 dim: 1 dim: 120 dim: 120 }\n",
    "  }\n",
    "}\n",
    "\"\"\".strip()\n",
    "predict_net = pb2.NetParameter()\n",
    "pl = text_format.Merge(text, predict_net)\n",
    "pl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c6c935e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(['data'], [[1, 1, 120, 120]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parse_input(predict_net)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "804c3df7",
   "metadata": {},
   "source": [
    "### 多输入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "adf88243",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "layer {\n",
       "  name: \"data1\"\n",
       "  type: \"Input\"\n",
       "  top: \"data1\"\n",
       "  input_param {\n",
       "    shape {\n",
       "      dim: 1\n",
       "      dim: 3\n",
       "      dim: 120\n",
       "      dim: 120\n",
       "    }\n",
       "  }\n",
       "}\n",
       "layer {\n",
       "  name: \"data2\"\n",
       "  type: \"Input\"\n",
       "  top: \"data2\"\n",
       "  input_param {\n",
       "    shape {\n",
       "      dim: 1\n",
       "      dim: 1\n",
       "      dim: 32\n",
       "      dim: 32\n",
       "    }\n",
       "  }\n",
       "}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text = \"\"\"\n",
    "layer {\n",
    "  name: \"data1\"\n",
    "  type: \"Input\"\n",
    "  top: \"data1\"\n",
    "  input_param {\n",
    "    shape: { dim: 1 dim: 3 dim: 120 dim: 120 }\n",
    "  }\n",
    "}\n",
    "\n",
    "layer {\n",
    "  name: \"data2\"\n",
    "  type: \"Input\"\n",
    "  top: \"data2\"\n",
    "  input_param {\n",
    "    shape: { dim: 1 dim: 1 dim: 32 dim: 32 }\n",
    "  }\n",
    "}\n",
    "\"\"\".strip()\n",
    "predict_net = pb2.NetParameter()\n",
    "pl = text_format.Merge(text, predict_net)\n",
    "pl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "386202f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pl.input"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e0fdc479",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(['data1', 'data2'], [[1, 3, 120, 120], [1, 1, 32, 32]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parse_input(predict_net)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed2000c5",
   "metadata": {},
   "source": [
    "## 旧的写法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f97d9d34",
   "metadata": {},
   "source": [
    "## 多输入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "cd3c5bcc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name: \"MultiInput\"\n",
       "input: \"data1\"\n",
       "input: \"data2\"\n",
       "input_shape {\n",
       "  dim: 1\n",
       "  dim: 3\n",
       "  dim: 224\n",
       "  dim: 224\n",
       "}\n",
       "input_shape {\n",
       "  dim: 1\n",
       "  dim: 1\n",
       "  dim: 112\n",
       "  dim: 112\n",
       "}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input_layer = \"\"\"\n",
    "name: \"MultiInput\"\n",
    "# 定义所有输入名称\n",
    "input: [\"data1\", \"data2\"]\n",
    "\n",
    "# 第一个输入的维度配置\n",
    "input_shape {\n",
    "  dim: 1    # 批次大小\n",
    "  dim: 3    # 通道数\n",
    "  dim: 224  # 高度\n",
    "  dim: 224  # 宽度\n",
    "}\n",
    "\n",
    "# 第二个输入的维度配置\n",
    "input_shape {\n",
    "  dim: 1    # 批次大小\n",
    "  dim: 1    # 通道数\n",
    "  dim: 112  # 高度\n",
    "  dim: 112  # 宽度\n",
    "}\n",
    "\"\"\".strip()\n",
    "predict_net = pb2.NetParameter()\n",
    "pl = text_format.Merge(input_layer, predict_net)\n",
    "pl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "07ea0341",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(['data1', 'data2'], [[1, 3, 224, 224], [1, 1, 112, 112]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parse_input(predict_net)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85a0c8b4",
   "metadata": {},
   "source": [
    "## 单输入\n",
    "\n",
    "有两种写法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6a20a647",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name: \"单输入\"\n",
       "input: \"data\"\n",
       "input_shape {\n",
       "  dim: 1\n",
       "  dim: 3\n",
       "  dim: 224\n",
       "  dim: 224\n",
       "}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input_layer = \"\"\"\n",
    "name: \"单输入\"\n",
    "# 定义所有输入名称\n",
    "input: \"data\"\n",
    "\n",
    "# 输入的维度配置\n",
    "input_shape {\n",
    "  dim: 1    # 批次大小\n",
    "  dim: 3    # 通道数\n",
    "  dim: 224  # 高度\n",
    "  dim: 224  # 宽度\n",
    "}\n",
    "\n",
    "\"\"\".strip()\n",
    "predict_net = pb2.NetParameter()\n",
    "pl = text_format.Merge(input_layer, predict_net)\n",
    "pl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a4d82956",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(['data'], [[1, 3, 224, 224]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parse_input(predict_net)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d1992f4c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name: \"单输入\"\n",
       "input: \"data\"\n",
       "input_dim: 1\n",
       "input_dim: 3\n",
       "input_dim: 32\n",
       "input_dim: 32"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input_layer = \"\"\"\n",
    "name: \"单输入\"\n",
    "# 定义所有输入名称\n",
    "input: \"data\"\n",
    "\n",
    "# 输入的维度配置\n",
    "input_dim: 1\n",
    "input_dim: 3\n",
    "input_dim: 32\n",
    "input_dim: 32\n",
    "\"\"\".strip()\n",
    "predict_net = pb2.NetParameter()\n",
    "pl = text_format.Merge(input_layer, predict_net)\n",
    "pl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "154add1e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(['data'], [[1, 3, 32, 32]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parse_input(predict_net)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10e77dcc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py313",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
